/*
 * Copyright (C) 2019 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.yoyo.fragment.connect;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xuexiang.yoyo.constant.HesAppStatic;
import com.xuexiang.yoyo.core.BaseFragment;
import com.xuexiang.yoyo.databinding.FragmentConnectBinding;
import com.xuexiang.yoyo.utils.LogHesUtil;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.utils.XToastUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.grouplist.XUICommonListItemView;
import com.xuexiang.xui.widget.grouplist.XUIGroupListView;

/**
 * @author liuxiangfei
 * @since 2023-07-24 00:19
 */
@Page(anim = CoreAnim.none)
public class ConnectFragment extends BaseFragment<FragmentConnectBinding> {
    @NonNull
    @Override
    protected FragmentConnectBinding viewBindingInflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, boolean attachToRoot) {
        return FragmentConnectBinding.inflate(inflater, container, attachToRoot);
    }

    /**
     * @return 返回为 null意为不需要导航栏
     */
    @Override
    protected TitleBar initTitle() {
        return super.initTitle().setTitle("Connect");
    }

    /**
     * 初始化控件
     */
    @Override
    protected void initViews() {

        initGroupListView();
        initNotConnectView();

        WidgetUtils.initRecyclerView(binding.recyclerView);

        itemWithSwitch.getSwitch().setChecked(HesAppStatic.isConnectFlag());

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onResume() {
        super.onResume();
        LogHesUtil.lxfBlue("ConnectFragment onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        LogHesUtil.lxfBlue("ConnectFragment onPause");
    }

    private void startScan() {
        LogHesUtil.lxfBlue("ConnectFragment startscan");

    }
    XUICommonListItemView itemWithSwitch;
    private void initGroupListView() {
        itemWithSwitch = binding.groupListView.createItemView("通讯方式");
        itemWithSwitch.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_SWITCH);
        itemWithSwitch.getSwitch().setOnCheckedChangeListener((buttonView, isChecked) ->{
            HesAppStatic.setConnectFlag(isChecked);
            LogHesUtil.lxfBlue("isChecked:"+isChecked);
            if(isChecked){
                startScan();
            }else{
                binding.connectView.setVisibility(View.GONE);
            }
            binding.notConnectView.setVisibility(isChecked?View.VISIBLE:View.GONE);
            binding.recyclerView.setVisibility(isChecked?View.VISIBLE:View.GONE);
        });

        int size = DensityUtils.dp2px(getContext(), 20);
        XUIGroupListView.newSection(getContext())
                .setLeftIconSize(size, ViewGroup.LayoutParams.WRAP_CONTENT)
                .addItemView(itemWithSwitch, onClickListener)
                .addTo(binding.groupListView);



    }

    XUICommonListItemView connectItem;

    private void initNotConnectView() {
        binding.notConnectView.setVisibility(View.GONE);

        int size = DensityUtils.dp2px(getContext(), 20);


        XUIGroupListView.newSection(getContext()).setTitle("可连接通讯")
                .setLeftIconSize(size, ViewGroup.LayoutParams.WRAP_CONTENT)
                .addTo(binding.notConnectView);
    }



    View.OnClickListener onClickListener = v -> {
        if (v instanceof XUICommonListItemView) {
            CharSequence text = ((XUICommonListItemView) v).getText();
            XToastUtils.toast(text + " is Clicked");
        }
    };

}
